package com.sxt;

import OwnSideObject.PlaneObj;

import java.awt.*;

/**
 * 动画实体类
 */
public class AnimationObj extends GameObject implements Runnable {

    @Override
    public void run() {
        while (PicNum>=0){
            this.paintSelf(this.Frame.offScreenImage.getGraphics());
            PicNum--;
            try {
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        System.out.println("animation is over");
    }

    @Override
    public void DeBug() {
        System.out.println("AnimationClass");
    }

    //    static Image[] images = null;
    Image[] images = null;
    //计数帧数
    int PicCount = 0;
    //动画帧数
    int PicNum;
    //图片subName
    String PicSubName;
    //图片格式
    String PicFormat;

    public AnimationObj(String picSubName, String picFormat, int picNum, int x, int y, GameWin frame) {
        super();
        this.PicSubName = picSubName;
        this.PicFormat = picFormat;
        this.PicNum = picNum;
        this.x = x;
        this.y = y;
        this.Frame = frame;
        images = new Image[picNum];
        for (int i = 0; i < picNum; i++) {
            images[i] = Toolkit.getDefaultToolkit().getImage(picSubName + (i + 1) + picFormat);
        }
    }

    public AnimationObj(String s, int i, int x, int y, GameWin gameWin) {
        this.x = x;
        this.y = y;
    }

    @Override
    public void paintSelf(Graphics s) {
        //动画播放效果
        if (PicCount < PicNum) {
            s.drawImage(images[PicCount], this.x, this.y, null);
//            System.out.println("animationPicNums:"+images.length+" location:X->"+this.x+" Y->"+this.y+" PicCount:"+PicCount);
//            System.out.println("animation is running");
            PicCount++;
        } else {
            this.Frame.removeList.add(this);
//            System.out.println("animation is  removed");
        }
    }


    //专门为State = 0时循环出现设计的
    public void paintSelfLoop(Graphics s) {
        //动画播放效果
        setX(GameWin.WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2);
        setY(GameWin.WINDOW_HEIGHT / 3 - GameWin.TOP_BORDER_HEIGHT);
        s.drawImage(images[PicCount], this.x, this.y, null);
        PicCount = (++PicCount) % PicNum;
    }

    @Override
    public Rectangle getRec() {
        return null;
    }

}
